package com.atguigu.tingshu.user.service.impl;

import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.user.UserPaidAlbum;
import com.atguigu.tingshu.model.user.UserPaidTrack;
import com.atguigu.tingshu.user.mapper.UserPaidAlbumMapper;
import com.atguigu.tingshu.user.mapper.UserPaidTrackMapper;
import com.atguigu.tingshu.user.service.UserPaidTrackService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.stream.Collectors;

/**
 * @author sun
 */
@Service
public class UserPaidTrackServiceImpl extends ServiceImpl<UserPaidTrackMapper, UserPaidTrack> implements UserPaidTrackService {

	@Resource
	private UserPaidAlbumMapper userPaidAlbumMapper;

	@Resource
	private UserPaidTrackMapper userPaidTrackMapper;

	/**
	 * 判断用户是否购买了专辑
	 *
	 * @param albumId 专辑id
	 * @return 是否购买
	 */
	@Override
	public Boolean isPaidAlbum(Long albumId) {
		return userPaidAlbumMapper.selectCount(
				new LambdaQueryWrapper<UserPaidAlbum>()
						.eq(UserPaidAlbum::getAlbumId, albumId)
						.eq(UserPaidAlbum::getUserId, AuthContextHolder.getUserId())) > 0;

	}

	/**
	 * 获取用户已购的音频id
	 *
	 * @param albumId 专辑id
	 * @param userId  用户id
	 * @return 用户已购的音频id
	 */
	@Override
	public HashSet<Long> getPaidTrackIds(Long albumId, Long userId) {
		return  (HashSet<Long>) userPaidTrackMapper.selectList(
				new LambdaQueryWrapper<UserPaidTrack>()
						.eq(UserPaidTrack::getAlbumId, albumId)
						.eq(UserPaidTrack::getUserId, userId)
		).stream().map(UserPaidTrack::getTrackId).collect(Collectors.toSet());
	}
}
